<template>
	<view class="content">
		<!-- <view class="login_header">
			<view class="fanhui" @click="go_back"><tui-icon name="arrowleft" :size="30" color="#333"></tui-icon></view>
		</view> -->
		<view class="login_img flex flex-center">
			<image src="/static/logo.png" mode="widthFix"></image>
		</view>
		<view class="view-tuichu" @click="wei_login">微信授权登录</view>
		<view class="login_agree flex" style=" padding-bottom: 5px;position: fixed;bottom: 9vh;left: 30rpx;z-index: 1111;box-sizing: border-box;width: 660rpx;">
			<checkbox :checked="is_check" style="transform: scale(.7);" @click="get_check" />
			<view class="">我已阅读并同意 <text @click="go_url('/pages/login/xieyi?type=1&&title=用户协议')">《用户协议》</text>和<text @click="go_url('/pages/login/xieyi?type=2&&title=隐私政策')">《隐私政策》</text>并同意登录</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				title: 'Hello',
				mobile:'',
				is_check:false,
				is_hei:false
			}
		},
		onLoad() {

		},
		methods: {
			go_back(){
				uni.navigateBack()
			},
			
			get_check(){
				let that = this
				that.is_check=!that.is_check
			},
			go_url(e){
				let that = this
				that.tui.href(e)
			},
			wei_login(){
				let that = this
				if(!that.is_check){
					that.tui.toast('请阅读用户协议和隐私政策');
					return;
				}
				uni.showLoading({
					title: '登陆中',
					mask: true
				});
				// #ifdef APP-PLUS
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						console.log(event)
						that.tui.request("v1/auth/wechatLogin", "POST", {code:event.code}, false, false, false).then((res) => {
							console.log(res)
							if(res.isBindMobile){
								that.tui.toast('登录成功')
								uni.setStorageSync('token', res.token)
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/index/index'
									})
								}, 1500)
							}else{
								that.tui.href('/pages/login/bind_mobile?wechatId='+res.wechat_id)
							}
						}).catch((res) => {
							that.is_btn = false
						})
					},
					fail: function (err) {
				        // 登录授权失败  
				        // err.code是错误码
				    }
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.getUserProfile ({
					desc: '登录后可同步数据',
					success: async (obj) => {
					console.log('obj', obj);
						// 调用 action ，请求登录接口
						// await this.login(obj);
						uni.login({
							provider: 'weixin',
							success: (res) => {
								let data = {code: res.code,inviter_code:uni.getStorageSync('refCode'),iv:obj.iv,encrypted:obj.encryptedData}
									console.log(data)
								that.tui.request("api.wxapp/decode", "POST", data, false, false, true).then((res) => {
									console.log(res)
									that.tui.toast('登录成功')
									uni.removeStorage({
									    key: 'refCode',
									    success: function (res) {
									        console.log('success');
									    }
									});
									uni.setStorageSync('d_token', res.data.token)
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									}, 1500)
								}).catch((res) => {
									that.is_btn = false
								})
							},
						});
					},
					fail: () => {
						uni.showToast({
							title: '授权已取消',
							icon: 'error',
							mask: true,
						});
					},
					complete: () => {
						// 隐藏loading
						uni.hideLoading();
					},
				});
				// #endif
			}
		}
	}
</script>

<style lang="scss">
	.login_img {text-align: center;
		image {
			width: 230rpx;
			height: 230rpx;
			margin-top: 230rpx;
			border-radius: 100%;
		}
	}
	.login_header{position: fixed;top: 0;left: 0;width: 100%;padding: 60rpx 0 0;
		.fanhui{padding-left: 20rpx;}
	}
	
	.view-tuichu{
	    height: 82rpx;
	    line-height: 82rpx;
	    text-align: center;
	    margin: 92rpx auto;
	    background: linear-gradient(90deg, #bbe68c, #bbe68c);
	    border-radius:40rpx;
	    font-size: 28rpx;
	    font-family: PingFang;
	    font-weight: 700;
	    color: #fff;box-sizing: border-box;
	}
	.view-tuichu {
		position: fixed;bottom: 17vh;left: 30rpx;width: 690rpx;
		border-radius: 10rpx;margin: 0;
	}
	.login_agree{
		text{color: #bbe68c;}
	}
</style>
